<?php

namespace App\Http\Controllers;

use Dingo\Api\Routing\Helpers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Validation\ValidationException;


class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests,Helpers;

    /**
     * @var string 模型
     */
    public $model;

    /**
     * @var 数据格式化
     */
    public $transformer;

    /**
     * @var
     */
    public $policy = false;

    /**
     * BaseController constructor
     * @param array $withs
     * @throws \Exception
     */
    public function __construct()
    {
        //resolve函数使用 服务容器 解析给定名称的类或接口的实例
        // Laravel 服务容器是一个用于管理类依赖以及实现依赖注入的强有力工具。
        // 依赖注入这个名词表面看起来花哨，实质上是指：通过构造函数，或者某些情况下通过「setter」方法将类依赖「注入」到类中。
        if (!empty($this->model)) {
            $this->query = resolve($this->model);
        }
    }


    /**
     * 获取model对象
     * @return mixed
     *
     */
    public function getQuery()
    {
        return $this->query;
    }

    /**
     * @param Request $request
     * @return |null
     * @throws ValidationException
     */
    public function validateData(Request $request)
    {
        //validate 方法可以让你验证用户凭证信息而不用真的登陆应用
        $this->validate($request, $this->rules($request), $this->ruleMessages(), $this->customAttributes());
    }


    /**
     * 处理搜索数据
     *
     * @param Request $request
     * @return Request
     */
    public function formatIndexQuery(Request $request)
    {
        $searchableModels = $this->getQuery()->searchableModels ?? [];

        if (empty($searchableModels)) return $request;

        foreach ($request->all() as $key => $value) {
            //data_get 函数使用「.」符号从多维数组或对象中根据指定键检索值,default支持默认值，没有返回默认值
            $field_model = data_get($searchableModels, $key, '');

            if (!empty($field_model)) {
                //str_replace() 函数替换字符串中的一些字符（区分大小写）。
                $val = str_replace('field', $value, $field_model);
                //添加到$request数组中
                $request->offsetSet($key, $val);
            }
        }

        return $request;
    }


    /**
     * 获取一页的数量
     *
     * @return mixed
     */
    public function getPageLimit()
    {
        $limit = request()->input('pageSize');

        if ($limit < 0 || empty($limit)) {
            $limit = config('paginate.limit');
        } elseif ($limit > 100) {
            $limit = 100;
        }

        return $limit;
    }


    /**
     * @param null $field
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function admin_user($field = null)
    {
        $user = auth('admin')->user();

        if (empty($field)) {
            return $user;
        }

        return $user->{$field};
    }

    /**
     * @param null $field
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function web_user($field = null)
    {
        $user = auth('web')->user();

        if (empty($field)) {
            return $user;
        }
        switch ($field){ //TODO:写前台的时候删掉switch，现在写死了
            case 'id':
                return 111;
                break;
            case 'nickname':
                return 'nickname';
                break;
            case 'avatar':
                return 'avatar';
                break;
        }
        return $user->{$field};
    }

    /**
     * 参数验证规则
     *
     * @param Request $request
     * @return array
     */
    public function rules(Request $request): array
    {
        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'POST':
                return [];
                break;
            case 'PUT':
                return [];
                break;
            case 'PATCH':
                return [];
                break;
        }
        return [];
    }

    /**
     * 验证信息
     *
     * @return array
     */
    public function ruleMessages(): array
    {
        return [];
    }

    /**
     * 字段
     *
     * @return array
     */
    public function customAttributes(): array
    {
        return [];
    }


}
